<?php

namespace backend\module\content\models;

use Yii;
use yii\helpers\ArrayHelper;
/**
 * This is the model class for table "{{%content_category}}".
 *
 * @property int $id
 * @property int $parent
 * @property string $name
 * @property string $icon
 * @property string $controller
 * @property string $description
 * @property string $keywords
 * @property int $userid
 *
 * @property ContentCategory $parent0
 * @property ContentCategory[] $contentCategories
 */
class ContentCategory extends \backend\models\Node
{
    static protected  $_data;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%content_category}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'name', 'controller', 'userid'], 'required'],
            [['parent', 'userid'], 'integer'],
            [['name', 'icon','keywords'], 'string', 'max' => 255],
            [['controller'], 'string', 'max' => 50],
            [['description'],'safe'],
            [['parent'], 'exist', 'skipOnError' => true, 'targetClass' => ContentCategory::className(), 'targetAttribute' => ['parent' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'Id'),
            'parent' => Yii::t('common', 'Parent'),
            'name' => Yii::t('common', 'Name'),
            'icon' => Yii::t('common', 'Icon'),
            'controller' => Yii::t('common', 'Controller'),
            'description' => Yii::t('common', 'Description'),
            'keywords' => Yii::t('common', 'Keywords'),
            'userid' => Yii::t('common', 'Content Category Userid'),
        ];
    }
  
    /**
     * 返回所有分类数据
     * @param int $userid  分类所属的用户ID
     * @return array 
     */
    static public function getAllData($userid){
        $data = self::$_data[$userid];
        if(empty($data)){
           //$data = self::find(['userid'=>$userid])->asArray()->all();
           $data = self::findAll(['userid'=>$userid]);
           self::$_data[$userid] = $data;
        }
        return $data;
    }
    /**
     * 记取指定控制器的最后一级节点
     * @param Array $data 节点数据
     * @param String $controller 指定的控制器
     * @param Int $parent 开始的节点父ID
     * @return Array
     */
    static public function getSubNodeModel($data, $controller = null,$parent = null){
        $node = [];
        foreach ($data as $v) {
            if($v['parent'] == $parent){
                $subNode = self::getSubNodeModel($data, $controller, $v['id']);
                if(empty($subNode) and (!$controller or $controller == $v['controller'])){
                   $node[$v['id']] = $v['name']; 
                }else{
                    $node = ArrayHelper::merge($node, $subNode);
                }
            }
        }
        return $node;
    }    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getParent0()
    {
        return $this->hasOne(ContentCategory::className(), ['id' => 'parent'])->all();
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getContentCategories()
    {
        return $this->hasMany(ContentCategory::className(), ['parent' => 'id'])->all();
    }
}
